﻿using System;
using System.Collections.Generic;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;

namespace SPRepository
{
    public static class TaxonomyFieldUtilities
    {
        public static TaxonomyFieldValueCollection GetValues(SPListItem item, TaxonomyField taxonomyField)
        {
            if (item == null)
                throw new ArgumentNullException("item");
            if (taxonomyField == null)
                throw new ArgumentNullException("taxonomyField");
            if (!taxonomyField.AllowMultipleValues)
                throw new InvalidOperationException("The taxonomy field does not allow multiple values");

            return item[taxonomyField.Id] as TaxonomyFieldValueCollection;
        }

        public static TaxonomyFieldValue GetValue(SPListItem item, TaxonomyField taxonomyField)
        {
            if (item == null)
                throw new ArgumentNullException("item");
            if (taxonomyField == null)
                throw new ArgumentNullException("taxonomyField");
            if (taxonomyField.AllowMultipleValues)
                throw new InvalidOperationException("The taxonomyfield allows multiple values");

            return item[taxonomyField.Id] as TaxonomyFieldValue;
        }

        public static void SetValue(SPListItem item, TaxonomyField taxonomyField, Term value)
        {
            if (item == null)
                throw new ArgumentNullException("item");
            if (taxonomyField == null)
                throw new ArgumentNullException("taxonomyField");

            if (value != null)
            {
                taxonomyField.SetFieldValue(item, value);
            }
            else
            {
                var taxonomyFieldValue = new TaxonomyFieldValue(taxonomyField);
                taxonomyField.SetFieldValue(item, taxonomyFieldValue);
            }
        }

        public static void SetValue(SPListItem item, TaxonomyField taxonomyField, Guid value)
        {
            if (item == null)
                throw new ArgumentNullException("item");
            if (taxonomyField == null)
                throw new ArgumentNullException("taxonomyField");

            var taxonomyFieldValue = new TaxonomyFieldValue(taxonomyField);
            taxonomyFieldValue.TermGuid = value.ToString("B");
            taxonomyField.SetFieldValue(item, taxonomyFieldValue);
        }

        public static void SetValues(SPListItem item, TaxonomyField taxonomyField, IEnumerable<Guid> values)
        {
            if (item == null)
                throw new ArgumentNullException("item");
            if (taxonomyField == null)
                throw new ArgumentNullException("taxonomyField");

            var taxonomyFieldValueCollection = new TaxonomyFieldValueCollection(taxonomyField);
            foreach (Guid value in values)
            {
                var taxonomyFieldValue = new TaxonomyFieldValue(taxonomyField);
                taxonomyFieldValue.TermGuid = value.ToString("B");
                taxonomyFieldValueCollection.Add(taxonomyFieldValue);
            }
            taxonomyField.SetFieldValue(item, taxonomyFieldValueCollection);
        }

        public static void SetValues(SPListItem item, TaxonomyField taxonomyField, List<Term> values)
        {
            if (item == null)
                throw new ArgumentNullException("item");
            if (taxonomyField == null)
                throw new ArgumentNullException("taxonomyField");
            if (!taxonomyField.AllowMultipleValues && values.Count > 1)
                throw new ArgumentException("The taxonomy field does not accept multiple values");

            if (values != null)
            {
                taxonomyField.SetFieldValue(item, values);
            }
            else
            {
                taxonomyField.SetFieldValue(item, new List<Term>());
            }
        }
    }
}